﻿using System;
namespace HomeWork
{
    public class Test
    {
        public Test()
        {
            AlarmClock clock = new AlarmClock();

            // Register event handlers
            clock.Tick += OnTick;
            clock.Alarm += OnAlarm;

            // Start the clock
            clock.Start();

            // Wait for user input
            Console.ReadLine();

            // Stop the clock
            clock.Stop();
        }
        static void OnTick(object sender, EventArgs e)
        {
            Console.WriteLine("Tick");
        }

        static void OnAlarm(object sender, EventArgs e)
        {
            Console.WriteLine("Alarm!");
        }
    }


    public class AlarmClock
    {
        public event EventHandler Tick;
        public event EventHandler Alarm;

        private bool running = false;

        public void Start()
        {
            running = true;

            while (running)
            {
                // 触发Tick事件
                if (Tick != null)
                {
                    Tick(this, EventArgs.Empty);
                }

                // 触发Alarm事件
                if (DateTime.Now.Second == 20)
                {
                    if (Alarm != null)
                    {
                        Alarm(this, EventArgs.Empty);
                    }
                }

                // Wait for 1 second
                System.Threading.Thread.Sleep(1000);
            }
        }
        public void Stop()
        {
            running = false;
        }

    }

}

